# import necessary packages
import argparse
import cv2 as cv

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="The Path to Image")
args = vars(ap.parse_args())

# load the image ,convert it to grayscale,and blur it slightly
image = cv.imread(args["image"])
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (7, 7), 0)
cv.imshow("Image", image)

# apply Otsu's automatic thresholding -- Otsu's method automaticaly
# determines the best threshold value `T` for us
(T, threshInv) = cv.threshold(blurred, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)

cv.imshow("Threshold", threshInv)
print("Otsu's thresholding vlaue: {}".format(T))

# finally,we can visualize only the masked regions in the image
cv.imshow("Output", cv.bitwise_and(image, image, mask=threshInv))
cv.waitKey(0)
